package com.example.markingsystem.controller;

import com.example.markingsystem.bean.user;
import com.example.markingsystem.mapper.LoginMapper;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.thymeleaf.util.StringUtils;

import java.lang.reflect.Array;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

@Controller
public class LoginController {
    @Resource
    LoginMapper loginMapper;

    @RequestMapping("index")
    public String codetest(){

        return "index";
    }

    //index2为被授权的网页
    @RequestMapping("index2")
    public String codetest2(){

        return "index";
    }


    @RequestMapping("/indexto")
    public String idcodelogin(String idcode, Model model, HttpSession session){
        //功能不全之前增加的监控
        //System.out.println("输进来的值是："+idcode);
        //判断数据库表中是否有这个idcode
        Boolean idexit = loginMapper.idexit(idcode);
        if(!idexit){
            //报告没有这个身份码时的操作
            model.addAttribute("reinfo","你的身份码错误!");
            return "index";
        }

        //获取身份码全部信息
        user nowuser = loginMapper.codegetalluse(idcode);
        //判断次数是否够用，如果够用则-1，否则返回
        if(nowuser.getNumberuse()>=1){
            //次数-1
            int fewer = loginMapper.numberuse111(idcode);
        }
        else{
            //报告没有次数的结果
            model.addAttribute("reinfo","你的使用次数不够！");
            return "index";
        }

        //登录成功且有效时，储存
        session.setAttribute("userlogin",idcode);

        //判断这个idcode是否为管理员
        if(nowuser.getIidentity()==1){
            //跳转管理员登录界面
            return "test";
        }

        //这之后需要判断是否已经填过了信息，填完了直接进入主页
        //联合查询三个信息表内是否含有其身份信息，有就直接跳转主页
        Boolean completely = loginMapper.checkidcode(idcode);
        if(completely){
            //直接跳转主页
            return "test";
        }

        String aaa = nowuser.getIdcode();
        //判断这个idcode属于管理员外的哪一个身份
        char thirdDigit = aaa.charAt(aaa.length() - 3);
        int digit = Character.getNumericValue(thirdDigit);

        if(digit==1){
            return "benxiaolaoshiInfo";
        }
        if(digit==5){
            return "yanjiushengInfo";
        }
        if(digit==8){
            //return "waixiaolaoshiInfo";
            return "teacherInfo";
        }

        //能走到这大概是不可能的，除非身份码不符合标准
        model.addAttribute("reinfo","你的身份码出现了未知的错误！请联系管理员！");
        return "index";
    }
}
